{extend name="../../mxadmin/view/base" /}

{block name="title"}图片广告管理{/block}

{block name="body"}
<style>
    .tb-img-circle {
        width: 60px;
        height: 60px;
        cursor: zoom-in;
        border-radius: 5%;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">标&emsp;题</label>
                        <div class="layui-input-inline">
                            <input name="title" class="layui-input" autocomplete="off" placeholder="输入标题关键词"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">广告分类</label>
                        <div class="layui-input-inline">
                            <select name="type">
                                <option value="">--- 全部 ---</option>
                                {volist name="adtype" id="vo"}
                                <option value="{$vo.id}">{$vo.name}</option>
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">状&emsp;态</label>
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="">--- 全部 ---</option>
                                <option value="1">启用</option>
                                <option value="0">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="mxTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="mxTable" lay-filter="mxTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="mxTbBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="mxTbState">
    <input type="checkbox" lay-filter="mxTbStateCk" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" {{d.status==1?'checked':''}} style="display: none;"/>
    <p style="display: none;">{{d.status==1?'启用':'禁用'}}</p>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="mxEditDialog">
    <form id="mxEditForm" lay-filter="mxEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">广告分类</label>
            <div class="layui-input-block">
                <select name="type" lay-verType="tips" lay-verify="required" required>
                    <option value="">请选择广告分类</option>
                    {volist name="adtype" id="vo"}
                    <option value="{$vo.id}">{$vo.name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">广告标题</label>
            <div class="layui-input-block">
                <input name="title" class="layui-input" autocomplete="off" placeholder="请输入广告标题" lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">广告图片</label>
            <div class="layui-input-block">
                <input name="image" class="layui-input" autocomplete="off" placeholder="请选择图片"/>
                <button type="button" class="layui-btn layui-btn-normal imageMore"><i class="layui-icon">&#xe681;</i>上传图片</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">广告链接</label>
            <div class="layui-input-block">
                <input name="url" class="layui-input" autocomplete="off" placeholder="请输入广告链接"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">打开方式</label>
            <div class="layui-input-block">
                <input type="radio" name="target" value="_self" title="本窗口" checked/>
                <input type="radio" name="target" value="_blank" title="新窗口"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序权重</label>
            <div class="layui-input-block">
                <input name="weight" placeholder="请输入排序权重" type="number" class="layui-input" autocomplete="off" lay-verType="tips" lay-verify="number" value="50"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="mxEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" mx-event="closeDialog">取消</button>
        </div>
    </form>
</script>
{/block}

{block name="script"}
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var upload = layui.upload;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#mxTable',
            url: "{:url('cms/ad/datalist')}",
            page: {limit: 15},
            limits: [15, 30, 45, 60, 75, 90, 100, 200, 500],
            toolbar: ['<p>',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>批量删除</button>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加广告</button>&nbsp;',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'title', title: '广告标题', align: 'center'},
                {
                    field: 'image', title: '广告图片', templet: function (d) {
                        if (d.image) {
                            return '<img src="' + d.image + '" class="tb-img-circle" tb-img alt="' + d.title + '"/>';
                        } else {
                            return '';
                        }
                    }, align: 'center', width: 90, unresize: true
                },
                {field: 'url', title: '广告链接', align: 'center'},
                {field: 'type_name', title: '广告分类', align: 'center'},
                {field: 'weight', title: '排序权重', align: 'center', width: 110, sort: true, edit: 'text'},
                {field: 'status', title: '状态', templet: '#mxTbState', align: 'center', sort: true, width: 100},
                {field: 'create_time', title: '创建时间', align: 'center', sort: true},
                {field: 'update_time', title: '更新时间', align: 'center', sort: true},
                {title: '操作', toolbar: '#mxTbBar', align: 'center', minWidth: 180}
            ]],
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
        });

        /* 点击图片放大 */
        $(document).off('click.tbImg').on('click.tbImg', '[tb-img]', function () {
            layer.photos({photos: {data: [{src: $(this).attr('src'), alt: $(this).attr('alt')}]}, shade: .5, closeBtn: true});
        });

        /* 表格搜索 */
        form.on('submit(mxTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}, url:"{:url('cms/ad/serach')}"});
            return false;
        });

        /* 表格操作列点击事件 */
        table.on('tool(mxTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 监听单元格编辑 */
        table.on('edit(mxTable)', function(obj){
            if(isNaN(obj.value)){
                layer.msg('只能输入数字！', {icon: 5, anim: 6});
                return false;
            }
            var loadIndex = layer.load(2);
            $.post("{:url('cms/ad/edit_weight_same')}", {
                id: obj.data.id,
                weight: obj.value
            }, function (res) {
                layer.close(loadIndex);
                if (res.code === 1) {
                    layer.msg(res.msg);
                    insTb.reload();
                } else {
                    layer.msg(res.msg, {icon: 5, anim: 6});
                }
            }, 'json');
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(mxTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('mxTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 5, anim: 6});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: '650px',
                title: (mData ? '修改' : '添加') + '广告',
                shade: 0.5,
                //shadeClose: true,
                content: $('#mxEditDialog').html(),
                success: function (layero, dIndex) {
                    //图片上传
                    upload.render({
                        elem: '.imageMore',
                        url: "{:url('mxadmin/upload/upload')}", //改成您自己的上传接口
                        acceptMime: 'image/*',
                        size: 2048, //限制文件大小，单位 KB
                        done: function(res){
                            if (res.code === 1) {
                                layer.msg(res.msg, {icon: 1});
                                this.item.prev('input').val(res.data.filePath);
                                this.item.after(' <a href="'+res.data.filePath+'" target="_blank"><img src="'+res.data.filePath+'" height="38"></a>');
                            } else {
                                layer.msg(res.msg, {icon: 5, anim: 6});
                            }
                        }
                    });

                    // 回显表单数据
                    form.val('mxEditForm', mData);
                    // 表单提交事件
                    form.on('submit(mxEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? "{:url('cms/ad/edit')}" : "{:url('cms/ad/add')}", data.field, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();  // 保存成功刷新表格
                            } else {
                                layer.msg(res.msg, {icon: 5, anim: 6});
                            }
                        }, 'json');
                        return false;
                    });

                    if (mData) {
                        // 上传图片小图显示
                        var imageValue = $('.imageMore').prev('input').val();
                        if (imageValue) {
                            $('.imageMore').after(' <a href="'+imageValue+'" target="_blank"><img src="'+imageValue+'" height="38"></a>');
                        }
                    }
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中的数据吗？', {
                title: '删除数据',
                skin: 'layui-layer-admin',
                shade: 0.5,
                shadeClose: true,
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post("{:url('cms/ad/del')}", {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 1) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.msg, {icon: 5, anim: 6});
                    }
                }, 'json');
            });
        }

        /* 修改状态 */
        form.on('switch(mxTbStateCk)', function (obj) {
            var loadIndex = layer.load(2);
            $.post("{:url('cms/ad/edit_state_same')}", {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.close(loadIndex);
                if (res.code === 1) {
                    layer.msg(res.msg);
                    insTb.reload();
                } else {
                    layer.msg(res.msg, {icon: 5, anim: 6});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });
    });
</script>
{/block}
